package com.zlt.bkparking.service.impl;

import com.zlt.bkparking.dto.AdvertisementDTO;
import com.zlt.bkparking.dto.AdvertisersUserDTO;
import com.zlt.bkparking.entity.Advertisement;
import com.zlt.bkparking.entity.AdvertisementExample;
import com.zlt.bkparking.entity.AdvertisersUser;
import com.zlt.bkparking.entity.AdvertisersUserExample;
import com.zlt.bkparking.mapper.AdvertisementMapper;
import com.zlt.bkparking.mapper.AdvertisersUserMapper;
import com.zlt.bkparking.service.AdvertisementService;
import com.zlt.bkparking.vo.AdvertisementVO;
import com.zlt.bkparking.vo.AdvertisersUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("advertisementService")
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private AdvertisersUserMapper advertisersUserMapper;

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Override
    public List<AdvertisersUserVO> showAdvertisersUser() {
        AdvertisersUserExample advertisersUserExample = new AdvertisersUserExample();
//        AdvertisersUserExample.Criteria criteria = advertisersUserExample.createCriteria();
        List<AdvertisersUser> advertisersUsers = advertisersUserMapper.selectByExample(advertisersUserExample);
        List<AdvertisersUserVO> advertisersUserVOS = new ArrayList<>();
        advertisersUsers.forEach(a -> {
            AdvertisersUserVO advertisersUserVO = new AdvertisersUserVO();
            BeanUtils.copyProperties(a, advertisersUserVO);
            advertisersUserVOS.add(advertisersUserVO);
        });
        return advertisersUserVOS;
    }

    @Override
    public AdvertisersUserVO addAdvertisersUser(AdvertisersUserDTO advertisersUserDTO) {
        AdvertisersUser advertisersUser = new AdvertisersUser();
        BeanUtils.copyProperties(advertisersUserDTO, advertisersUser);
        // 插入数据并返回auId
        advertisersUserMapper.insertSelectiveReturnAuId(advertisersUser);
        Long auId = advertisersUser.getAuId();
//        System.out.println(auId);
        // 通过auId查询该信息
        AdvertisersUser advertisersUser1 = advertisersUserMapper.selectByPrimaryKey(auId);
        AdvertisersUserVO advertisersUserVO = new AdvertisersUserVO();
        BeanUtils.copyProperties(advertisersUser1, advertisersUserVO);
        return advertisersUserVO;
    }

    @Override
    public List<AdvertisementVO> showAdvertisementList() {
        AdvertisementExample advertisementExample = new AdvertisementExample();
        List<Advertisement> advertisements = advertisementMapper.selectByExample(advertisementExample);
        List<AdvertisementVO> advertisementVOS = new ArrayList<>();
        advertisements.forEach(a -> {
            AdvertisersUser advertisersUser = advertisersUserMapper.selectByPrimaryKey(a.getAuId());
            AdvertisersUserVO advertisersUserVO = new AdvertisersUserVO();
            BeanUtils.copyProperties(advertisersUser, advertisersUserVO);
            AdvertisementVO advertisementVO = new AdvertisementVO();
            BeanUtils.copyProperties(a, advertisementVO);
            advertisementVO.setAdvertisersUserVO(advertisersUserVO);
            advertisementVOS.add(advertisementVO);
        });
        return advertisementVOS;
    }

    @Override
    public boolean addAdvertisement(AdvertisementDTO advertisementDTO) {
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(advertisementDTO, advertisement);
        //advertisement.setAdDayRemain(advertisement.getAdDayCount());
        int i = advertisementMapper.insertSelective(advertisement);
        return i>0;
    }

    @Override
    public AdvertisersUserVO queryAdvertiserByAuId(Long auId) {
        System.out.println(auId);
        AdvertisersUser advertisersUser = advertisersUserMapper.selectByPrimaryKey(auId);
        AdvertisersUserVO advertisersUserVO = new AdvertisersUserVO();
        BeanUtils.copyProperties(advertisersUser, advertisersUserVO);
        return advertisersUserVO;
    }

    @Override
    public boolean undercarriageAdvertisement(Long adId) {
        Advertisement advertisement = advertisementMapper.selectByPrimaryKey(adId);
        advertisement.setAdState((byte) 2);
        int i = advertisementMapper.updateByPrimaryKeySelective(advertisement);
        return i>0;
    }

    @Override
    public List<Advertisement> getAdvertisementByToday() {
        AdvertisementExample example = new AdvertisementExample();
        example.createCriteria().andAdEndGreaterThanOrEqualTo(new Date()).andAdStateEqualTo((byte) 0);
        return advertisementMapper.selectByExample(example);
    }
}
